package com.sword.demo.quartz.util;

import org.quartz.JobDetail;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean;

/**
 * Quartz工具类
 * @author sword
 * @date 2022/11/17 20:27
 */
public class QuartzUtil {
    /**
     * 初始化一个定时任务详情
     * @param targetObject 目标类对象
     * @param targetMethod 目标方法
     * @return org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean 定时详情
     * @author wukongjian
     * @date 2019/10/16 9:58
     */
    public static MethodInvokingJobDetailFactoryBean createJobDetail(Object targetObject, String targetMethod) {
        MethodInvokingJobDetailFactoryBean jobDetailFactoryBean = new MethodInvokingJobDetailFactoryBean();
        // 设置目标类对象
        jobDetailFactoryBean.setTargetObject(targetObject);
        // 设置目标方法
        jobDetailFactoryBean.setTargetMethod(targetMethod);
        // 防止并发执行
        jobDetailFactoryBean.setConcurrent(false);
        return jobDetailFactoryBean;
    }

    /**
     * 初始化一个定时任务触发器
     * @param jobDetail 定时任务详情
     * @param cronExpression 触发器执行时间表达式
     * @return 定时任务触发器
     * @author wukongjian
     * @date 2019/10/16 10:04
     */
    public static CronTriggerFactoryBean createCronTrigger(JobDetail jobDetail, String cronExpression) {
        CronTriggerFactoryBean cronTriggerFactoryBean = new CronTriggerFactoryBean();
        // 设置定时任务详情
        cronTriggerFactoryBean.setJobDetail(jobDetail);
        // 设置执行时间表达式
        cronTriggerFactoryBean.setCronExpression(cronExpression);
        return cronTriggerFactoryBean;
    }
}
